<template>
  <div class="page flex-column">
    <div class="page-title">
      <span class="location-part"><a href="/#/selected-content/list">精选内容</a></span>
      <span class="location-split">/</span><span class="location-current">全部资源</span>
    </div>
    <div v-if="isNotEmpty(tagList)" class="material-filter">
      <tag-selector
        v-model:checkedTagIds="checkedTagIds"
        :tags="tagList"
      />
    </div>
    <div class="material-result flex-column">
      <div v-if="isNotEmpty(tagList)" class="result-title">资料列表</div>
      <a-flex v-if="isNotEmpty(storeAllMaterial.materialSource)" wrap="wrap" gap="20" class="result-list">
        <material-card v-for="item in storeAllMaterial.materialSource" :key="item.id"
          :title="item.name"
          price="1299"
          @onDownload="doDownload(item.id, item.url)">
        </material-card>
      </a-flex>
      <div v-else class="empty-list">
        <div class="empty-box">
          <img src="@/assets/images/empty-course.png" />
          <div class="empty-tip">
            <div class="empty-label">无当前标签内容</div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="totalRecords>pageSize" class="material-pagination flex-row flex-center">
      <a-pagination 
        v-model:current="currentPage"
        v-model:pageSize="pageSize"
        :total="totalRecords"
        @change="handlePageChange"
        show-less-items />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onBeforeMount, watch } from 'vue';
import { isNotEmpty } from "@/utils";
import useAllMaterialStore from '@/stores/all-material';
import TagSelector from '@components/TagSelector.vue';
import MaterialCard from '@components/MaterialCard.vue';
import { downloadFile } from '@/utils/download'
import { miscApi } from "@/services";
 
const storeAllMaterial = useAllMaterialStore();
const currentPage = ref(storeAllMaterial.pageNum);
const pageSize = ref(storeAllMaterial.pageSize);
const totalRecords = ref(0);
const checkedTagIds = ref([]);
const tagList = ref([]);

onBeforeMount(async () => {
  goTop()
  await storeAllMaterial.initData();

  totalRecords.value = storeAllMaterial.totalRecords;
  tagList.value = storeAllMaterial.tagList;

  await miscApi.traceViewPage("/selected-content/materials");
});

watch(
  checkedTagIds, // 直接监听 ref
  async () => {
    storeAllMaterial.checkedTagIds = checkedTagIds.value;
    await storeAllMaterial.handleSearch();

    totalRecords.value = storeAllMaterial.totalRecords;
  },
  { deep: true }
);

const doDownload = async (id: number, url:string) => {
  if (!url) {
    return;
  }

  try {
    const fileName = url.split('/').pop();
    await downloadFile({
      url: url,
      fileName: fileName // 可选自定义文件名‌:ml-citation{ref="6" data="citationList"}
    })

    if (id) {
      // 保存到我的资料
      await storeAllMaterial.addAsMyMaterial(id);
    }
  } catch (error) {
    console.error(`下载文件 ${url} 出现异常`, error);
  }
}

const handlePageChange = (page, pageSize) =>{
  storeAllMaterial.handlePageChange(page, pageSize)
  goTop()
}
const goTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth' 
  });
}
</script>

<style lang="less" scoped>
.page {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background-color: @white-bg;

  .page-title {
    height: 22px;
    line-height: 22px;
    font-size: 16px;
    font-weight: 400;
    padding: 20px 30px;

    .location-part {
      cursor: pointer;
    }

    .location-split {
      padding: 0 2px;
    }

    .location-part, .location-split {
      color: @font-grey-light;
    }

    .location-current {
      color: @font-black;
    }
  }

  .material-filter {
    background-color: @white;
    border-radius: 20px;
    margin: 20px 30px 0 30px;
    padding: 22px 20px 10px 20px
  }
  .material-result {
    background-color: @white;
    border-radius: 20px;
    margin: 20px 30px;

    .result-title {
      height: 24px;
      line-height: 24px;
      font-size: 20px;
      font-weight: 500;
      color: @font-black;
      padding: 20px;
    }

    .result-list {
      margin: 25px;
    }

    .empty-list {
      height: 940px;
      display: flex;
      flex-direction: column;
      align-items: center;

      .empty-box {
        width: 510px;
        height: 309px;
        margin-top: 120px;
        position: relative;
        display: flex;
        justify-content: center;

        img {
          width: 100%;
          height: 100%;
        }

        .empty-tip {
          position: absolute;
          bottom: 0px;
          height: 16px;
          line-height: 16px;
          font-size: 16px;
          font-weight: 400;
          display: flex;
          flex-direction: row;
          align-items: center;

          .empty-label {
            color: @font-grey;
          }

          .empty-btn {
            color: @text-link-color;
          }
        }
      }
    }
  }
  .material-pagination {
    width: 100%;
    padding: 10px 30px 50px 30px;
  }
}

:deep(.ant-pagination) {
  .ant-pagination-item {
    min-width: 44px;
    height: 44px;
    margin-inline-end: 10px;
    line-height: 44px;
    background-color: @white;
    border: 1px solid @border-red-light;
    border-radius: 50%;

    a {
      font-size: 14px;
      font-weight: 400;
    }

    &.ant-pagination-item-active {
      border: 1px solid #ED171F;
    }
  }
  .ant-pagination-prev, .ant-pagination-next, .ant-pagination-jump-prev, .ant-pagination-jump-next {
    min-width: 44px;
    height: 44px;
    color: @font-black;
    line-height: 44px;
    border: 1px solid @border-red-light;
    border-radius: 50%;
    margin-inline-end: 10px;

    a {
      font-size: 14px;
      font-weight: 400;
      border-radius: 50%;
    }
  }

  .ant-pagination-prev:hover .ant-pagination-item-link, .ant-pagination-next:hover .ant-pagination-item-link {
    background-color: rgba(0, 0, 0, 0.06);
  }

  .ant-pagination-prev .ant-pagination-item-link, .ant-pagination-next .ant-pagination-item-link {
    border-radius: 50%;
  }

  .ant-pagination-options {
    display: none;
  }
}
</style>
